<?php
namespace Modules\User\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\User\Entities\User;

use \Modules\Log\Services\LogService;

use Validator;
use ThrowException;
use DB;
use Auth;

class UserController extends Controller
{
    public function statusTotal(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $total = [
            'wechat' => User::type('wechat')->count(),
            'kuaishou' => User::type('kuaishou')->count(),
            'alipay' => User::type('alipay')->count(),
            'web' => User::type('web')->count(),
            'app' => User::type('app')->count(),
            'marked' => User::type('marked')->count(),
            'blocked' => User::blocked()->count()
        ];

        return [
            'total' => $total
        ];
    }

    // 用户列表
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $options['with'] = ['inviter'];
        // $options['with_count'] = ['invitees'];

        // if ($req->input('sort')) {
        //     if ($req->sort === 'invitee')
        //         $options['order_by'] = ['invitees_count' => 'desc'];
        // }

        $options['only'] = [
            'id',
            'uuid',
            'name',
            'real_name',
            'number',
            'card_number',
            'phone',
            'gender',
            'headimg',
            'email',
            'birthday',
            'city',
            'area',
            'score',
            'level_score',
            'redpack',
            'level_text',
            'client_type',
            'status',
            'invitees_count',
            'last_active_at',
            'created_at',
            'lucky_score',
            'remark',
            'tags',
            'marked_at',
            'direct_invitees_count',
            'indirect_invitees_count',
            'inviter' => [
                'id',
                'uuid',
                'name',
                'headimg'
            ]
        ];

        $list = User::getList($options);

        $list->each(function ($item) {
            $item->score = $item->asset('score')->balance;
            $item->level_score = $item->asset('level_score')->balance;
            $item->redpack = $item->asset('redpack')->balance;
        });

        return $list->generateListResponse();
    }

    // 单个用户详情
    public function show(Request $req, $uuid)
    {
        $item = User::where('uuid', $uuid)->orWhere('id', $uuid)->first() ?: ThrowException::Conflict('用户不存在~');

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    }

    // 更新用户信息
    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $admin = Auth::requireLoginAdmin();

        $item = User::getEntity([
            'uuid' => $uuid
        ]);

        DB::beginTransaction();

        if ($req->type === 'update') {
            $info = $req->input('attributes');
            $item->update($info);
        }
        elseif ($req->type === 'mark') {
            if ($item->marked_at) {
                $item->update([
                    'marked_at' => NULL
                ]);
            }
            else {
                $item->update([
                    'marked_at' => now()
                ]);
            }
        }
        elseif ($req->type === 'block') {
            $item->update([
                'status' => 2,
                'blocked_at' => now()
            ]);
        }
        elseif ($req->type === 'unblock') {
            $item->update([
                'status' => 1,
                'blocked_at' => null
            ]);
        }
        elseif ($req->type === 'update_fudai_lucky_score') {

            if (is_null($req->input('value'))) {
                ThrowException::Conflict('请填写欧气值~');
            }

            $before = $item->lucky_score;
            $after = $req->value;

            if ($before == $after) {
                ThrowException::Conflict('更新前后值不能一样~');
            }

            // 更新福袋欧气值
            $item->update([
                'lucky_score' => $req->value
            ]);

            LogService::log('AdminLog', [
                'admin_id' => $admin->id,
                'user_id' => $item->id,
                'asset_type' => 'fudai_lucky_score',
                'action_type' => 1,
                'describe' => '更新福袋欧气值',
                'options' => [
                    'before' => $before,
                    'after' => $after
                ]
            ]);
            
        }

        DB::commit();

        return [];
    }

    // 删除用户
    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = User::getEntity([
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }
}
